package com.woniu.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.common.entity.AdmAccount;
import com.woniu.common.entity.AdmCustomer;
import com.woniu.login.mapper.AccountMapper;
import com.woniu.login.mapper.CustomerMapper;
import com.woniu.login.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    AccountMapper accountMapper;
    //  查询所有客户
    @Override
    public IPage<AdmCustomer> getAll(int page, int size, AdmCustomer admCustomer) {
        System.out.println("admCustomer："+admCustomer);
        Page<AdmCustomer> npage = new Page<>(page, size);
        QueryWrapper<AdmCustomer> queryWrapper = new QueryWrapper<AdmCustomer>().eq("isdelete", 0);
        // 检查name是否不为空，如果非空，则添加like条件
        if (StringUtils.isNotBlank(admCustomer.getName())) {
            queryWrapper.like("name", admCustomer.getName());
        }
        if (StringUtils.isNotBlank(admCustomer.getPhone())) {
            queryWrapper.like("phone", admCustomer.getPhone());
        }
        if (StringUtils.isNotBlank(admCustomer.getGender())) {
            queryWrapper.like("gender", admCustomer.getGender());
        }
        if (StringUtils.isNotBlank(admCustomer.getLevel())) {
            queryWrapper.like("level", admCustomer.getLevel());
        }
        if (StringUtils.isNotBlank(admCustomer.getEducation())) {
            queryWrapper.like("education", admCustomer.getEducation());
        }
        if (StringUtils.isNotBlank(admCustomer.getAddr())) {
            queryWrapper.like("addr", admCustomer.getAddr());
        }
        if (StringUtils.isNotBlank(admCustomer.getAge())) {
            queryWrapper.like("addr", admCustomer.getAddr());
        }
        if (admCustomer.getCreatetime1() != null && admCustomer.getCreatetime2() != null) {
            // 使用ge和le方法来设置时间范围的查询条件
            queryWrapper.ge("create_time", admCustomer.getCreatetime1()); // 大于等于createTime1
            queryWrapper.le("create_time", admCustomer.getCreatetime2()); // 小于等于createTime2
        }

        IPage<AdmCustomer> customerPage = customerMapper.selectPage(npage, queryWrapper);
        return customerPage;
    }
    //  添加客户
    @Override
    public void addCustomer(AdmCustomer admCustomer) {
//        // 截取身份证号中的出生日期
//        LocalDate birthDate = LocalDate.parse(admCustomer.getIdcard().substring(6, 14));
//        // 计算年龄
//        int age = Period.between(birthDate, LocalDate.now()).getYears();
//        admCustomer.setAge(age);
        System.out.println(admCustomer);
        //  获取默认昵称
        String uuid = UUID.randomUUID().toString();
        String user = uuid.substring(0, 4) + "_" + uuid.substring(4, 10);
        admCustomer.setUser(user);

        admCustomer.setCreatetime1(LocalDate.now());
        admCustomer.setCreatetime2(LocalDate.now());
        customerMapper.insert(admCustomer);
        AdmAccount admAccount = new AdmAccount();
        admAccount.setAauser(user);
        admAccount.setAaname(admCustomer.getName());
        admAccount.setAaemail(admCustomer.getEmail());
        admAccount.setAatype("客户");
        accountMapper.insert(admAccount);
    }
    //  批量删除客户
    @Override
    public void deletebatch(Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        customerMapper.deleteByIds(idList);
    }
    //  删除客户
    public void deleteCustomer(int id){
        customerMapper.deleteById(id);
    }
    //  修改客户
    @Override
    public void updateCustomer(AdmCustomer admCustomer) {
        QueryWrapper<AdmCustomer> queryWrapper = new QueryWrapper<AdmCustomer>().eq("acid", admCustomer.getAcid());
        customerMapper.update(admCustomer,queryWrapper);
    }
    //  根据id查询客户
    @Override
    public void getCustomerById(int id) {
        customerMapper.selectById(id);
    }

    ;
}
